package com.wing.robot.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.robot.dao.RobotInstructDao;
import com.wing.robot.model.entity.RobotInstruct;
import com.wing.robot.model.request.RobotInstructForm;
import com.wing.robot.model.response.RobotInstructVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 机器人调度指令类型-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class RobotInstructServiceImpl extends ServiceImpl<RobotInstructDao, RobotInstruct> implements RobotInstructService {

    @Override
    public int add(RobotInstructForm robotInstructForm) {

        //校验

        RobotInstruct robotInstruct =new RobotInstruct();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(RobotInstruct.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(robotInstructForm, robotInstruct, copyOptions);

        return baseMapper.insert(robotInstruct);
    }

    @Override
    public List<RobotInstructVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<RobotInstructVO> pageList4app(Page page, Map<String, Object> params) {
        List<RobotInstructVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public RobotInstructVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int enable(Long id) {
        RobotInstructVO robotInstructVO = baseMapper.get4appById(id);
        if (robotInstructVO!=null){
            RobotInstruct robotInstruct = new RobotInstruct();
            robotInstruct.setId(id);
            robotInstruct.setIsDeleted(true);
            int updateCount =baseMapper.updateById(robotInstruct);
            return updateCount;
        }
        return 0;
    }

    @Override
    public int disable(Long id) {
        RobotInstructVO robotInstructVO = baseMapper.get4appById(id);
        if (robotInstructVO!=null){
            RobotInstruct robotInstruct = new RobotInstruct();
            robotInstruct.setId(id);
            robotInstruct.setIsDeleted(false);
            int updateCount =baseMapper.updateById(robotInstruct);
            return updateCount;
        }
        return 0;
    }
}
